<?php

	if (!class_exists('ctl_cart'))
	{
		require_once(CORE_DIR.'/shop/controller/ctl.cart.php');
	}

	class diamond_shop_cct_cart extends ctl_cart
	{
		function diamond_shop_cct_cart(&$system)
		{
			parent::ctl_cart($system);
			$this->_verifyMember(false);
			if(!$this->system->getConf('system.use_cart',true)){
				$system->responseCode(404);
				echo '<h1>cart has been disabled</h1>';
				exit();
			}
			$this->objCart = &$this->system->loadModel('trading/cart');
			$this->objCart->checkMember($this->member);
			
			if($_POST['isfastbuy']){
				if($_POST['goods']){
					$aParams = $this->objCart->getParams($_POST['goods']);
					$this->cart = $this->objCart->setFastBuy('g', $aParams);
					if ($_POST['bare']['goods_id'])
					{
						$aBareParams = $this->objCart->getParams($_POST['bare']);
						$bareCart = $this->objCart->setFastBuy('g', $aBareParams);
						foreach ($bareCart['g']['cart'] as $k =>$v)
						{
							$this->cart['g']['cart'][$k] = $v;
						}
						if ($_POST['bare']['minfo'])
						{
							$this->bare_minfo = urldecode($_POST['bare']['minfo']);
							setcookie('S[BARE_MINFO]', $this->bare_minfo);
						}
						else $this->bare_minfo = urldecode($_COOKIE['BARE_MINFO']);
					}
					setcookie('S[Cart_Fastbuy]', $this->objCart->_save($this->cart));
				}else{
					$this->cart = $this->objCart->getCart('all',$_COOKIE['Cart_Fastbuy']);
				}
			}else{
				$this->cart = $this->objCart->getCart('all');
			}
			//error_log(print_r($this->cart,true),3,__FILE__.'.'.__FUNCTION__.'.cart.log');//alone add
			$this->products = $this->cart['g'];
			$this->pkggoods = $this->cart['p'];
			$this->gifts = $this->cart['f'];
		}

		function checkout($isfastbuy=0)
		{
			if($isfastbuy){
				$this->cart = $this->objCart->getCart('all',$_COOKIE['Cart_Fastbuy']);
				$this->products = $this->cart['g'];
				$_POST['isfastbuy'] = 1;
			}
			$this->title = __('填写购物信息');
			if(count($this->products['cart'])+count($this->pkggoods)+count($this->gifts) == 0){
				$this->redirect('cart');
				exit;
			}

			if(!$this->member['member_id'] && !$_COOKIE['ST_ShopEx-Anonymity-Buy']){
				$this->redirect('cart','loginBuy',array($_POST['isfastbuy']));
				exit;
			}
			
			$aOut = $this->objCart->getCheckout($this->cart, $this->member, $this->system->request['cur']);
			$this->pagedata['has_physical'] = $aOut['has_physical'];
			$this->pagedata['minfo'] = $aOut['minfo'];
			$this->pagedata['areas'] = $aOut['areas'];
			$this->pagedata['dlytime'] = date('Y-m-d', time()+floatval($this->system->getConf('site.delivery_time'))*3600*24);
			$this->pagedata['currencys'] = $aOut['currencys'];
			$this->pagedata['currency'] = $aOut['currency'];
			$payment = $this->system->loadModel('trading/payment');
			$payment->showPayExtendCon($aOut['payments']);
			$this->pagedata['payments'] = $aOut['payments'];
			if ($aOut['payments']){
				foreach($aOut['payments'] as $key => $val){
					if(!$this->member['member_id'] && $val['pay_type'] == 'deposit'){
						unset($this->pagedata['payments'][$key]);
						continue;
					}
					$this->pagedata['payments'][$key]['config']=unserialize($val['config']);
				}
			}
			$this->pagedata['config'] = unserialize($aOut['payments']['config']);
			$this->pagedata['trading'] = $aOut['trading'];

			if($this->member['member_id']){
				$member = &$this->system->loadModel('member/member');
				$addrlist = $member->getMemberAddr($this->member['member_id']);
				foreach($addrlist as $rows){
					if(empty($rows['tel'])){
						$str_tel = __('手机：').$rows['mobile'];
					}else{
						$str_tel = __('电话：').$rows['tel'];
					}
					$addr[] = array('addr_id'=> $rows['addr_id'],'def_addr'=>$rows['def_addr'],'addr_region'=> $rows['area'],
									'addr_label'=> $rows['addr'].__(' (收货人：').$rows['name'].' '.$str_tel.__(' 邮编：').$rows['zip'].')');
				}
				$this->pagedata['trading']['receiver']['addrlist'] = $addr;
				$this->pagedata['is_allow'] = (count($addr)<5 ? 1 : 0);
			}else{
				setcookie('S[ST_ShopEx-Anonymity-Buy]','',time()-1000);
			}
			//error_log(print_r($_COOKIE,true),3,__FILE__.'.'.__FUNCTION__.'._COOKIE.log');//alone add
			$this->pagedata['bare_minfo'] = $this->bare_minfo;
			$this->pagedata['checkout_base_path'] = dirname(__FILE__).'/view/shop/cart/checkout_base.html';
			$this->pagedata['_MAIN_'] = dirname(__FILE__).'/view/shop/cart/checkout.html';
			//error_log(print_r($this->pagedata,true),3,__FILE__.'.'.__FUNCTION__.'.pagedata.log');//alone add
			$this->output();
		}

		function shipping()
		{
			//error_log(print_r($_POST,true),3,__FILE__.'.'.__FUNCTION__.'.log');//alone add
			$sale = &$this->system->loadModel('trading/sale');
			$trading = $sale->getCartObject($this->cart,$GLOBALS['runtime']['member_lv'],true);
			$shipping = &$this->system->loadModel('trading/delivery');
			$aShippings = $shipping->getDlTypeByArea($_POST['area']);
			foreach($aShippings as $k=>$s){
				$aShippings[$k]['price'] = cal_fee($s['expressions'],$trading['weight'],$trading['pmt_b']['totalPrice'],$s['price']);
				$s['pad']==0?$aShippings[$k]['has_cod'] = 0:$aShippings[$k]['has_cod'] = 1;
				if($s['protect']==1){
					$aShippings[$k]['protect'] = true;
				}else{
					$aShippings[$k]['protect'] = false;
				}
			}
			$this->pagedata['shippings'] = $aShippings;
			$this->display('cart/checkout_shipping.html');
		}

		function total()
		{
			//error_log(print_r($_POST,true),3,__FILE__.'.'.__FUNCTION__.'.log');//alone add
			$tarea = explode(':', $_POST['area'] );
			$_POST['area'] = $tarea[count($tarea)-1];
			$this->pagedata['trading'] = $this->objCart->checkoutInfo($this->cart, $this->member, $_POST);
			$this->__tmpl='cart/checkout_total.html';
			$this->output();
		}
	}
?>